<template>
  <view class="goodsList">
    <view v-for="(item,i) in goodsList" :key="i" @click="goDetail(item)">
      <my_goods :good="item"></my_goods>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [], //商品总数
        total: 0, //数据总数
        flag: false, //节流阀
      };
    },
    // 获取参数
    onLoad(option) {
      // console.log(option);
      this.queryObj.query = option.query || '',
        this.queryObj.cid = option.cid || '',
        this.getGoodsList()
    },
    methods: {
      // 获取数据
      async getGoodsList(cb) {
        this.flag = true;
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
         this.flag = false
          cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        // console.log(res);
        this.goodsList = [...this.goodsList, ...res.message.goods]
        // this.goodsList = res.message.goods;
        this.total = res.message.total;   
      },
      // 去往详情页
      goDetail(good){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+good.goods_id
        })
      }
    },
    // 上拉刷新
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
      this.queryObj.pagenum++;
      if (this.flag == false) {
        this.getGoodsList()
      }
    },
    // 下拉刷新
    onPullDownRefresh() {
       this.queryObj.pagenum=1;
       this.total=0;
       this.flag=false;
       this.goodsList=[];
        this.getGoodsList(() => {
          uni.stopPullDownRefresh()
        })
    }
  }
</script>

<style scoped lang="less">
</style>
